package tp.pr4.commands;

import java.util.StringTokenizer;

import tp.pr4.Game;
import tp.pr4.MensajesConst;
import tp.pr4.Player;
import tp.pr4.Room;
import tp.pr4.commands.exceptions.CommandExecutionException;
import tp.pr4.commands.exceptions.WrongCommandFormatException;
import tp.pr4.items.Item;

public class DropCommand implements Command
{

	private Player _friki;
	private Room _habitacion;
	private String _itemToBeDroppedId;

	public DropCommand() {
		this._friki=null;
		this._habitacion=null;
		this._itemToBeDroppedId=null;
		
	}
	
	@Override public void execute() throws CommandExecutionException
	{
		Item playerItem = this._friki.getItem(this._itemToBeDroppedId);
		if (playerItem == null)
			throw new CommandExecutionException(MensajesConst.cantDrop+ this._itemToBeDroppedId + ".\n");
		else if (this._habitacion.existsItem(this._itemToBeDroppedId))
			throw new CommandExecutionException("");
		this._habitacion.addItem(this._friki.getItem(this._itemToBeDroppedId));
		this._friki.removeItem(this._itemToBeDroppedId.toUpperCase());
		

	}

	@Override public String getHelp()
	{
		return "DROP | SOLTAR <<id>>\n";
	}

	@Override public Command parse(String cad, Game execContext)
			throws WrongCommandFormatException
	{

		StringTokenizer commandTkn = new StringTokenizer(cad);
		if (commandTkn.countTokens() != 2)
			throw new WrongCommandFormatException();
		String com1 = commandTkn.nextToken();

		if (com1.equalsIgnoreCase("DROP") || com1.equalsIgnoreCase("soltar"))
		{
			this._friki = execContext.getPlayer();
			this._habitacion = execContext.getCurrentRoom();
			this._itemToBeDroppedId = commandTkn.nextToken();
			return this;
		}

		throw new WrongCommandFormatException();

	}

}
